package com.example.myworkmangeruse

import android.content.Context
import android.util.Log
import androidx.work.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext

/**
 * @author tgw
 * @date 2021/6/28
 * @describe
 */
class UpdateWorker(context: Context, workerParams: WorkerParameters) :
    CoroutineWorker(context, workerParams) {


    /**
     * Android认为Success和Failure都属于终止类的通知。
     * 意思是，如果发出这类通知，则表明任务彻底结束，而周期任务不会彻底终止，会一直执行下去
     *
     * 执行成功返回Result.success()
    执行失败返回Result.failure()
    需要重新执行返回Result.retry()
     */
    override suspend fun doWork(): Result {

        /**携程用法*/
        var result = withContext(Dispatchers.IO) {
            val imageUriInput =
                inputData.getString("IMAGE_URI") ?: return@withContext Result.failure()
            val result = getImage(imageUriInput)
            return@withContext result
        }
        return result;
//        //接收传递的参数
//        val imageUriInput =
//            inputData.getString("IMAGE_URI") ?: return Result.failure()
//
//
//        // Do the work here--in this case, upload the images.
//        Log.d("TAGtgw 工作", "doWork: " + imageUriInput)
//        Thread.sleep(2000)
//        //
//        var doWorkP = workDataOf("IMAGE_URI" to "http://...doWork返回 ")
//        // Indicate whether the work finished successfully with the Result
//        return Result.success(doWorkP)
    }

    private fun getImage(url: String): Result {
        val imageUriInput =
            inputData.getString("IMAGE_URI") ?: return Result.failure()


        // Do the work here--in this case, upload the images.
        Log.d("TAGtgw UpdateWorker工作", "doWork: " + imageUriInput)
//        Thread.sleep(2000)
        //
        var doWorkP = workDataOf("IMAGE_URI" to "http://...doWork返回 ")
        // Indicate whether the work finished successfully with the Result
        return Result.retry()
    }
}